<!-- pages/login/components/mobile.vue -->
<script setup lang="ts">
  import { getCodeApi, loginByMobileApi } from '@/api/user'
  import { useUserStore } from '@/stores/user'
  import { ref } from 'vue'

  defineOptions({
    options: {
      styleIsolation: 'shared',
    },
  })
  // 1. 倒计时处理
  const isShowCountDown = ref(false)
  const isAgree = ref(false)
  const getMobileCode = async () => {
    isShowCountDown.value = true
    await form.value?.validateField?.('mobile')
    const res = await getCodeApi({
      mobile: formData.value.mobile,
      type: 'login',
    })
    formData.value.code = res.code
  }

  // 表单数据
  const formData = ref({
    mobile: '13230000083',
    code: '',
  })
  const form = ref<UniHelper.UniFormsInstance>()
  // 验证表单数据的规则
  const formRules: UniHelper.UniFormsRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请填写手机号码' },
        { pattern: '^1[0-9]{10}$', errorMessage: '手机号码格式不正确' },
      ],
    },
    code: {
      rules: [
        { required: true, errorMessage: '请输入验证码' },
        { pattern: '^[0-9]{6}$', errorMessage: '验证码格式不正确' },
      ],
    },
  }
  const onSubmit = async () => {
    try {
      // 校验表单所有字段
      await form.value?.validate?.()
      // 判断是否同意用户协议
      if (!isAgree.value) {
        return uni.showToast({ title: '请勾选同意用户协议', icon: 'none' })
      }
      const res = await loginByMobileApi(formData.value)
      const user = useUserStore()
      user.setToken(res.token)
      // 保存用户 id
      user.userId = res.id
      uni.showToast({
        title: '登录成功',
        icon: 'none',
      })
      uni.switchTab({ url: '/pages/index/index' })
    } catch (error) {
      console.log(error)
    }
  }
</script>

<template>
  <uni-forms class="login-form" ref="form" :model="formData" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
        v-model="formData.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
        v-model="formData.code"
      />
      <text class="text-button" v-if="isShowCountDown === false" @click="getMobileCode"
        >获取验证码</text
      >
      <view class="text-button" v-else>
        <uni-countdown
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :second="5"
          @timeup="isShowCountDown = false"
        ></uni-countdown>
        秒后重新获取
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" @click="isAgree = !isAgree" color="#16C2A3" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onSubmit()">登 录</button>
  </uni-forms>
</template>

<style lang="scss">
  @import './styles.scss';
</style>
